package myta.bean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlSelectBooleanCheckbox;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.model.SelectItem;

import myta.base.FormDataBase;
import myta.model.DataRow;
import myta.model.DataTable;
import myta.service.Command;
import myta.service.SQLexec;

public class AbsensiData extends FormDataBase{
	private HtmlInputText txtid;
	private HtmlInputText txttanggal;
	private HtmlSelectOneMenu txtnis;
	private HtmlSelectOneMenu txtidjadwal;
	private HtmlSelectBooleanCheckbox txtmasuk;
	
	private List<SelectItem> listNis;
	private List<SelectItem> listJadwal;
	
	@Override
	public void init() throws Exception {
		formName = "absensi";
		
		txtid = new HtmlInputText();
		txttanggal = new HtmlInputText();
		txtnis = new HtmlSelectOneMenu();
		txtidjadwal = new HtmlSelectOneMenu();
		txtmasuk = new HtmlSelectBooleanCheckbox();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		txttanggal.setValue(sdf.format(Calendar.getInstance().getTime()));
		
		loadList();
		loadJadwal("");
	}
	
	private void loadList() throws Exception{
		listNis = new ArrayList<SelectItem>();
		listJadwal = new ArrayList<SelectItem>();
		
		SelectItem s = new SelectItem();
		s.setValue("-");
		s.setLabel("-");
		
		listNis.add(s);
		
		String sql = "select nis,nama from siswa order by nis";
		Command command = SQLexec.makeCommand(sql);
		DataTable dt = command.executeQuery();
		
		for(int i=0;i<dt.getRowCount();i++){
			DataRow dr = dt.getRow(i);
			
			s = new SelectItem();
			s.setValue(dr.getString("nis"));
			s.setLabel(dr.getString("nama"));
			
			listNis.add(s);
		}
		
		
	}
	
	private void loadJadwal(String nis) throws Exception{
		if(nis.equals("")){
			String sql = "select j.idjadwal,m.namamapel from jadwal j inner join mapel m on j.idmapel = m.idmapel inner join kelassiswa ks on j.idkelassiswa = ks.idkelassiswa order by j.idjadwal";
			Command command = SQLexec.makeCommand(sql);
			DataTable dt = command.executeQuery();
			
			for(int i=0;i<dt.getRowCount();i++){
				DataRow dr = dt.getRow(i);
				
				SelectItem s = new SelectItem();
				s.setValue(dr.getString("idjadwal"));
				s.setLabel(dr.getString("namamapel"));
				
				listJadwal.add(s);
			}
		}else{
			listJadwal = new ArrayList<SelectItem>();
			String date = txttanggal.getValue().toString();
			String[] arrayDate = date.split("-");
			Calendar cal = Calendar.getInstance();
			cal.set(Integer.valueOf(arrayDate[0]), Integer.valueOf(arrayDate[1]) - 1, Integer.valueOf(arrayDate[2]));
			Integer day = cal.get(Calendar.DAY_OF_WEEK);
			String hari = "";
			
			if(day == 2){
				hari = "Senin";
			}else if(day == 3){
				hari = "Selasa";
			}else if(day == 4){
				hari = "Rabu";
			}else if(day == 5){
				hari = "Kamis";
			}else if(day == 6){
				hari = "Jumat";
			}else if(day == 7){
				hari = "Sabtu";
			}else if(day == 1){
				hari = "Minggu";
			}
			
			String sql = "select j.idjadwal, m.namamapel from jadwal j ";
			sql += "inner join mapel m on j.idmapel = m.idmapel ";
			sql += "inner join kelassiswa ks on j.idkelassiswa = ks.idkelassiswa ";
			sql += "inner join kelassiswa_detail ksd on ks.idkelassiswa = ksd.idkelassiswa ";
			sql += "inner join siswa s on ksd.nis = s.nis ";
			sql += "where s.nis = ? and j.hari = ?";
			
			Command command = SQLexec.makeCommand(sql);
			command.setObject(1, txtnis.getValue().toString());
			command.setObject(2, hari);
			DataTable dt = command.executeQuery();
			
			for(int i=0;i<dt.getRowCount();i++){
				DataRow dr = dt.getRow(i);
				
				SelectItem s = new SelectItem();
				s.setValue(dr.getString("idjadwal"));
				s.setLabel(dr.getString("namamapel"));
				
				listJadwal.add(s);
			}
		}
	}

	public void afterNisSelected() throws Exception{
		loadJadwal("ada");
	}
	
	@Override
	public void generateID() throws Exception {
		String sql = "call idabsensi()";
		Command command = SQLexec.makeCommand(sql);
		DataTable datatable = command.executeQuery();
		DataRow datarow = datatable.getRow(0);
		txtid.setValue(datarow.getString("id"));
	}

	@Override
	public void getRequestData(Map<String, String> map) throws Exception {
		if(map.containsKey("idabsensi")){
			String idabsensi = map.get("idabsensi");
			
			String sql = "select * from absensi where idabsensi = ?";
			
			Command command = SQLexec.makeCommand(sql);
			command.setObject(1, idabsensi);
			DataTable datatable = command.executeQuery();
			DataRow datarow = datatable.getRow(0);
			
			txtid.setValue(datarow.getString("idabsensi"));
			txttanggal.setValue(datarow.getString("tglabsensi"));
			txtnis.setValue(datarow.getString("nis"));
			txtidjadwal.setValue(datarow.getString("idjadwal"));
			txtmasuk.setValue(datarow.getInteger("masuk") == 0 ? false : true);
		}
	}

	@Override
	public boolean simpan() throws Exception {
		boolean boo = false;
		
		String sql = "INSERT INTO absensi(idabsensi,tglabsensi,nis,idjadwal,masuk) VALUES(?,?,?,?,?);";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtid.getValue().toString());
		command.setObject(2, txttanggal.getValue().toString());
		command.setObject(3, txtnis.getValue().toString());
		command.setObject(4, txtidjadwal.getValue().toString());
		command.setObject(5, Boolean.valueOf(txtmasuk.getValue().toString()) == true ? 1 : 0);
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}		
	}

	@Override
	public boolean ubah() throws Exception {
		boolean boo = false;
		
		String sql = "UPDATE absensi SET tglabsensi=?,nis=?,idjadwal=?,masuk=? where idabsensi = ?;";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txttanggal.getValue().toString());
		command.setObject(2, txtnis.getValue().toString());
		command.setObject(3, txtidjadwal.getValue().toString());
		command.setObject(4, Boolean.valueOf(txtmasuk.getValue().toString()) == true ? 1 : 0);
		command.setObject(5, txtid.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}
	}

	public HtmlInputText getTxtid() {
		return txtid;
	}

	public void setTxtid(HtmlInputText txtid) {
		this.txtid = txtid;
	}
	
	public HtmlInputText getTxttanggal() {
		return txttanggal;
	}

	public void setTxttanggal(HtmlInputText txttanggal) {
		this.txttanggal = txttanggal;
	}

	public HtmlSelectOneMenu getTxtnis() {
		return txtnis;
	}

	public void setTxtnis(HtmlSelectOneMenu txtnis) {
		this.txtnis = txtnis;
	}

	public HtmlSelectOneMenu getTxtidjadwal() {
		return txtidjadwal;
	}

	public void setTxtidjadwal(HtmlSelectOneMenu txtidjadwal) {
		this.txtidjadwal = txtidjadwal;
	}

	public HtmlSelectBooleanCheckbox getTxtmasuk() {
		return txtmasuk;
	}

	public void setTxtmasuk(HtmlSelectBooleanCheckbox txtmasuk) {
		this.txtmasuk = txtmasuk;
	}

	public List<SelectItem> getListNis() {
		return listNis;
	}

	public void setListNis(List<SelectItem> listNis) {
		this.listNis = listNis;
	}

	public List<SelectItem> getListJadwal() {
		return listJadwal;
	}

	public void setListJadwal(List<SelectItem> listJadwal) {
		this.listJadwal = listJadwal;
	}
}
